package find.chat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.content.ServiceConnection;
import android.content.ComponentName;
import android.content.Context;
import android.os.IBinder;

import find.chat.R;

public class GPSActivity extends Activity{
	
	public LocationManager manager;
	private Location currentLocation; 
	private String best;
    private Button gpsButton;
    public AlertDialog gps = null; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gps_main);
        //gpsButton = (Button) findViewById(R.id.button1);

        manager = (LocationManager)getSystemService(LOCATION_SERVICE); 
    	// Check whether GPS is enable
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
        	// Display a dialog to enable GPS 
	        AlertDialog.Builder builder = new AlertDialog.Builder(this); 
	        builder.setTitle("Location Management") 
	        .setMessage("GPS is currently disable.\n\n" 
	               +"Enable GPS now?") 
	        .setPositiveButton("Enable", new DialogInterface.OnClickListener() { 
	            @Override 
	            public void onClick(DialogInterface dialog, int which) { 
	            	// Use intent object to change GPS setting
	                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
	                startActivity(i); 
	            } 
	        }) 
	        .setNegativeButton("Disable", null);
	        gps = builder.create();
	        gps.show();
		}
    }
    @Override
	protected void onResume() {
		super.onResume();
		// Obtain Best Location Provider
		Criteria criteria = new Criteria();
		best = manager.getBestProvider(criteria, true);
		
        int minTime = 5000; // in mini second
        float minDistance = 5; // in meter
		if (best != null) { // get last known location if any, using best provider 
	       currentLocation = manager.getLastKnownLocation(best); 
	       manager.requestLocationUpdates(best, minTime,
	    		                     minDistance, listener);
		}
		else { // get last known location if any, using GPS provider
           currentLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
           manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                   minTime, minDistance, listener);

		}
	    updatePosition(); // Update Position
	    //gpsButton.performClick();
	}
	@Override
	protected void onPause() {
		super.onPause();
		manager.removeUpdates(listener); 
	}
	// Update Position 
    private void updatePosition() { 
    	TextView output;
    	output = (TextView) findViewById(R.id.output);
        if (currentLocation == null) { 
            output.setText("Obtaining Location Information..."); 
        } else { 
            output.setText(getLocationInfo(currentLocation)); 
        } 
    } 
    // Creat Location Listener Object
    private LocationListener listener = new LocationListener() { 
        @Override 
        public void onLocationChanged(Location location) { 
            currentLocation = location; 
            updatePosition(); 
        }  
        @Override 
        public void onProviderDisabled(String provider) { } 
        @Override 
        public void onProviderEnabled(String provider) { } 
        @Override 
        public void onStatusChanged(String provider, int status, Bundle extras) { } 
    }; 
    // Getting Location Information
	public String getLocationInfo(Location location) {
		StringBuffer str = new StringBuffer();
		str.append("Provider: "+location.getProvider());
		str.append("\nLatitude: " + Double.toString(location.getLatitude()));
		str.append("\nLongitude: " + Double.toString(location.getLongitude()));
		str.append("\nAltitude: " + Double.toString(location.getAltitude()));
		return str.toString();
	}
	// Open Google Map
    public void button1_Click(View view) {
    	// Get latitude and longitude
    	float latitude = (float) currentLocation.getLatitude();
    	float longitude = (float) currentLocation.getLongitude();   
    	
    	String uri = String.format("geo:%f,%f?z=18", latitude, longitude);
    	// Create Intent Object
    	Intent geoMap = new Intent(Intent.ACTION_VIEW,Uri.parse(uri));
    	startActivity(geoMap);  // Open Google Map 	
    }

}
